package com.scau.ums.controller;


import com.scau.ums.model.Repairreport;
import com.scau.ums.model.User;
import com.scau.ums.service.UserInfo;
import com.scau.ums.service.factory.UserInfoImpFactory;
import com.scau.ums.service.imp.repair.UserRepairReportImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;

//提交报修单
@Controller
public class RepairreportController {

    @Autowired
    private UserRepairReportImp userRepairReportImp;

    @Autowired
    private UserInfoImpFactory userInfoImpFactory;

    @RequestMapping(value = "/repairreport",method = RequestMethod.GET)
    public String torepairreport(HttpServletRequest request)
    {
        String reporterId= (String) request.getSession().getAttribute("USER_ID");
        Integer reporterType= (Integer) request.getSession().getAttribute("USER_TYPE");
        UserInfo userInfo=userInfoImpFactory.getUserInfoImp(reporterType);
        User user=userInfo.getUser(reporterId);
        user.setPassword(null);
        request.setAttribute("user",user);
        return "repairreport";
    }

    @RequestMapping("/repairreport")
    @ResponseBody
    public String repairreport(Repairreport rep, HttpServletRequest request) throws ParseException
    {

            String reporterId=request.getParameter("reporterId");
            String reporterName=request.getParameter("reporterName");
            String reporterType=request.getParameter("reporterType");
            String telephone=request.getParameter("telephone");
            String dormRegion=request.getParameter("dormRegion");
            String edormBuilding=request.getParameter("dormBuilding");
            int dormBuilding=Integer.parseInt(edormBuilding);
            String edormRoom=request.getParameter("dormRoom");
            int dormRoom=Integer.parseInt(edormRoom);
            String desc=request.getParameter("desc");
            String etype=request.getParameter("type");
            int type=Integer.parseInt(etype);
            Date date = new Date();
            String remark="";//用户不需要备注
            String mail=request.getParameter("mail");

            if(userRepairReportImp.newRepairReport(reporterId,reporterName,reporterType,
                                                    telephone,dormRegion,dormBuilding,dormRoom,
                                                    desc,type,date,mail,remark))
            {
//                return "forward:/WEB-INF/jsp/repairreport.jsp";
                return "success";
//                System.out.println("提交保修单成功");
//                  return "redirect:repairreport";//返回WEB-INF/jsp/repairreport.jsp
            }

            else
            {
                return "error";
//                  return "redirect:repairreport";
            }
    }
}